package com.iwhalecloud.uncc.utils;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.compress.compressors.CompressorInputStream;
import org.apache.commons.compress.compressors.CompressorOutputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
import org.apache.commons.compress.compressors.gzip.GzipCompressorOutputStream;
import org.apache.commons.lang.exception.ExceptionUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;

@Slf4j
public final class ZipUtil {
	private ZipUtil() {

	}

	public static byte[] zip(byte[] bytes) {
		ByteArrayOutputStream out = null;
		CompressorOutputStream cos = null;
		try {
			out = new ByteArrayOutputStream();
			cos = new GzipCompressorOutputStream(out);
			cos.write(bytes);
			cos.close();

			byte[] rs = out.toByteArray();
			return rs;
		} catch (IOException e) {
			log.error(ExceptionUtils.getStackTrace(e));
		} finally {
			try {
				if (out != null) {
					out.close();
				}
			} catch (IOException e) {
				log.error(ExceptionUtils.getStackTrace(e));
			}
		}
		return "".getBytes();
	}

	
	
	/**
	 * @param bytes
	 * @return
	 */
	public static byte[] uzip(byte[] bytes) {
		CompressorInputStream cis = null;
		ByteArrayInputStream bais = null;
		ByteArrayOutputStream baos = null;
		try {
			bais = new ByteArrayInputStream(bytes);
			cis = new GzipCompressorInputStream(bais);
			baos = new ByteArrayOutputStream();
			byte[] buffer = new byte[1024];
			int size = 0;
			while ((size = cis.read(buffer)) > 0) {
				baos.write(buffer, 0, size);
			}
			byte[] rs = baos.toByteArray();
			return rs;
		} catch (IOException e) {
			log.error(ExceptionUtils.getStackTrace(e));
		} finally {
			if (baos != null) {
				try {
					baos.close();
				} catch (IOException e) {
					log.error(ExceptionUtils.getStackTrace(e));
				}
			}
			if (cis != null) {
				try {
					cis.close();
				} catch (IOException e) {
					log.error(ExceptionUtils.getStackTrace(e));
				}
			}
			if (bais != null) {
				try {
					bais.close();
				} catch (IOException e) {
					log.error(ExceptionUtils.getStackTrace(e));
				}
			}
		}
		return "".getBytes();
	}

	/**
	 * 使用gzip进行压缩
	 *
	 * @param primStr primStr
	 * @return string
	 */
	public static String gzip(String primStr) {
		if (primStr == null || primStr.length() == 0) {
			return primStr;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		GZIPOutputStream gzip = null;
		try {
			gzip = new GZIPOutputStream(out);
			gzip.write(primStr.getBytes());
		} catch (IOException e) {
			log.error(ExceptionUtils.getStackTrace(e));
		} finally {
			if (gzip != null) {
				try {
					gzip.close();
				} catch (IOException e) {
					log.error(ExceptionUtils.getStackTrace(e));
				}
			}
		}
		return new sun.misc.BASE64Encoder().encode(out.toByteArray());
	}

	/**
	 * 使用gzip进行解压缩
	 *
	 * @param compressedString compressedString
	 * @return string
	 */
	public static String ungzip(String compressedString) {
		if (compressedString == null) {
			return null;
		}
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		ByteArrayInputStream in = null;
		GZIPInputStream ginzip = null;
		byte[] compressed = null;
		String decompressed = null;
		try {
			compressed = new sun.misc.BASE64Decoder().decodeBuffer(compressedString);
			in = new ByteArrayInputStream(compressed);
			ginzip = new GZIPInputStream(in);
			byte[] buffer = new byte[1024];
			int offset = -1;
			while ((offset = ginzip.read(buffer)) != -1) {
				out.write(buffer, 0, offset);
			}
			decompressed = out.toString();
		} catch (IOException e) {
			log.error(ExceptionUtils.getStackTrace(e));
		} finally {
			if (ginzip != null) {
				try {
					ginzip.close();
				} catch (IOException ignored) {
				}
			}
			if (in != null) {
				try {
					in.close();
				} catch (IOException ignored) {
				}
			}
			try {
				out.close();
			} catch (IOException ignored) {
			}
		}
		return decompressed;
	}

}
